package com.xyhuo.leetcode.january;

import com.xyhuo.leetcode.common.TreeNode;

/**
 * 对称二叉树
 * https://leetcode.cn/problems/symmetric-tree/
 *
 * @time 2024/1/5
 */
public class IsSymmetric {

//    public static void main(String[] args) {
//        IsSymmetric is = new IsSymmetric();
//        TreeNode t1 = new TreeNode(1);
//        TreeNode t2_1 = new TreeNode(2);
//        TreeNode t2_2 = new TreeNode(2);
//        TreeNode t3_1 = new TreeNode(3);
//        TreeNode t3_2 = new TreeNode(3);
//        TreeNode t4_1 = new TreeNode(4);
//        TreeNode t4_2 = new TreeNode(4);
//
//        t1.left = t2_1;
//        t2_1.right = t3_1;
//
//        t1.right = t2_2;
//        t2_2.right = t3_2;
//
//
//        is.isSymmetric(t1);
//    }

    public boolean isSymmetric(TreeNode root) {
        search(root, root);
        return ans;
    }

    private boolean ans = true;

    private void search(TreeNode left, TreeNode right) {
        if (!ans || (left == null && right == null)) {
            return;
        }
        if ((left == null && right != null)
                || (left != null && right == null)) {
            ans = false;
            return;
        }
        if (left != null && right != null) {
            if (left.val != right.val) {
                ans = false;
                return;
            }
        }
        search(left.left, right.right);
        search(left.right, right.left);
    }
}
